package com.gdufs.secondhandwebsite.service.impl;

import com.gdufs.secondhandwebsite.dao.PostMapper;
import com.gdufs.secondhandwebsite.entity.Post;
import com.gdufs.secondhandwebsite.service.PostService;
import com.gdufs.secondhandwebsite.service.ex.UserNotLoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PostServiceImpl implements PostService {
    @Autowired
    PostMapper postMapper;
   public void createPost(Post post, String username, String userImg){
       if (username == null) {
           // 是：抛出UserNotFoundException异常
           throw new UserNotLoginException("用户尚未登录");
       }
        post.setUserName(username);
        post.setUserImg(userImg);
        postMapper.CreatePost(post);
    }
   public boolean delPost(Long postId){
       int result = postMapper.DeletePostByPrimaryKey(postId);
      if(result>0) {
          return true;
      }else{
          return false;
      }
   }
   public boolean updatePost(Post post){
       int result = postMapper.UpdatePostSelective(post);
       if(result>0) {
           return true;
       }else{
           return false;
       }
    }
    public List<Post> getAllPost(){
       return postMapper.getAllPost();
    }
    public Post getPostById(Long postId){
       return postMapper.SelectPostByPrimaryKey(postId);
    }
}
